package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.adapter.ClassifyAdapter;
import com.lsy.weizhi2.adapter.HomeNotesAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.swipetoloadlayout.BaseRecyclerAdapter;
import com.lsy.weizhi2.swipetoloadlayout.OnLoadMoreListener;
import com.lsy.weizhi2.swipetoloadlayout.OnRefreshListener;
import com.lsy.weizhi2.swipetoloadlayout.SuperRefreshRecyclerView;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/3/15.
 */
public class Home_Recommend extends Fragment implements OnRefreshListener, OnLoadMoreListener {
    private Context context;
    private RecyclerView home_notes_rv;
    private HomeNotesAdapter homeNotesAdapter;
    private SuperRefreshRecyclerView home_hot_rv;
    private ClassifyAdapter classifyAdapter;
    private Gson gson;
    private List<NoteList.ListBean> list_n;
    private int pageNo=1;
    private Handler handler=new Handler(Looper.getMainLooper());
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.home_notes,container,false);
        //home_notes_rv= (RecyclerView) view.findViewById(R.id.home_notes_rv);
        home_hot_rv = (SuperRefreshRecyclerView) view.findViewById(R.id.home_hot_rv);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        gson=new Gson();
        list_n=new ArrayList<>();
       /* LinearLayoutManager manager=new LinearLayoutManager(context);
        home_notes_rv.setLayoutManager(manager);
        homeNotesAdapter=new HomeNotesAdapter(context,null);
        home_notes_rv.setAdapter(homeNotesAdapter);*/

        home_hot_rv.init(new LinearLayoutManager(context),this,this);
        home_hot_rv.setRefreshing(true);
        home_hot_rv.setLoadingMoreEnable(true);
        classifyAdapter = new ClassifyAdapter(list_n,context);
        home_hot_rv.setAdapter(classifyAdapter);
        home_hot_rv.showData();
        classifyAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {
                Gson gson = new Gson();
                String json = gson.toJson(list_n.get(position));
                MyMethod.toActivity(context, NoteDetail.class, new String[]{"json"}, new String[]{json});
            }
        });
    }
    boolean bottom;
    @Override
    public void onResume() {
        super.onResume();
       /* home_notes_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                boolean islast=home_notes_rv.canScrollVertically(1);
                if(newState==RecyclerView.SCROLL_STATE_DRAGGING&&!islast){
                    if(list_n.size()==(pageNo)*10){
                        homeNotesAdapter.UpData(list_n,true,1);
                    }else{
                        homeNotesAdapter.UpData(list_n,true,3);
                    }
                    bottom=true;
                }else if(bottom&&newState==RecyclerView.SCROLL_STATE_IDLE&&!islast){
                    if(list_n.size()==(pageNo)*10){
                        homeNotesAdapter.UpData(list_n,true,2);
                        pageNo++;
                        getNotes();
                    }else{
                        homeNotesAdapter.UpData(list_n,true,3);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                homeNotesAdapter.UpData(list_n,false,0);
                            }
                        },1000);
                    }
                    bottom=false;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });*/
        pageNo=1;
        getNotes();
    }
    private void getNotes(){
        Log.i("Home_Recommend","token=="+ MyInfo.Token);
        Map<String,String> map=new HashMap<>();
        map.put("orderLevel","2");//推荐传2
        map.put("pageNo",pageNo+"");
        map.put("pageSize","10");
        MyOkHttpUtils.getInstance(context).doGet(Constants.ORDERLIST,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("Home_Recommend","message==="+message);
                        if(message!=null){
                            try{
                                NoteList noteList=gson.fromJson(message,NoteList.class);
                                if(pageNo==1){
                                    list_n.clear();
                                }
                                List<NoteList.ListBean>list=noteList.getList();
                                if(list!=null&&list.size()!=0){
                                    list_n.addAll(list);
                                    classifyAdapter.notifyDataSetChanged();
                                    //homeNotesAdapter.UpData(list_n,false,0);
                                    if(pageNo==1){
                                        home_hot_rv.setRefreshing(false);
                                    }else{
                                        home_hot_rv.setLoadingMore(false);
                                        home_hot_rv.moveToPosition(list_n.size() - 1);
                                    }
                                }else{
                                    if(pageNo==1){
                                        home_hot_rv.setRefreshing(false);
                                    }else{
                                        home_hot_rv.setLoadingMore(false);
                                        home_hot_rv.moveToPosition(list_n.size() - 1);
                                    }
                                    pageNo--;
                                   /* homeNotesAdapter.UpData(list_n,true,3);
                                    handler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            homeNotesAdapter.UpData(list_n,false,0);
                                        }
                                    },1000);*/
                                }
                            }catch (Exception e){
                                Log.i("Home_Recommend","message=错误=note="+message);
                            }
                        }
                    }
                });
    }
    @Override
    public void onRefresh() {
        pageNo=1;
        getNotes();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        getNotes();
    }

}
